package com.cdel.akka.supervisor;

import scala.Option;
import akka.actor.UntypedActor;

public class RestartActor extends UntypedActor {

	public enum Msg{
		DONE, RESTART;
	}

	@Override
	public void preStart(){
		System.out.println("preStart hashcode:" + this.hashCode());
	}

	@Override
	public void postStop(){
		System.out.println("postStop hashcode:" + this.hashCode());
	}

	@Override
	public void postRestart(Throwable reason) throws Exception{
		super.postRestart(reason);
		System.out.println("postRestart hashcode:" + this.hashCode());
	}

	@Override
	public void preRestart(Throwable reason, Option opt) throws Exception{
		System.out.println("preRestart hashcode:" + this.hashCode());
	}

	@Override
	public void onReceive(Object msg) throws Throwable {
		if(msg == Msg.DONE){
			getContext().stop(getSelf());
		}else if(msg == Msg.RESTART){
			//这里会抛出NullPointerException
			System.out.println(((Object)null).toString());
			//抛出异常ArithmeticException，默认会被restart，但这里会resume
			double a = 0/0;
		}
		unhandled(msg);
	}


}
